package Application;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;

import Methods.*;


import comm.AppInterface;

public class SampleRequest 
{
   private String idDevice;
   private String idMeasure;
   private String value;
	 
   public SampleRequest(String a,String b, String c)
   {
	  idDevice   = a;
	  idMeasure  = b;
	  value      = c;
   }
   
   public void GenerateRequest()
   {
	  HttpClient httpclient = new DefaultHttpClient();
	  RequestMethods request;
	  
	  //Param 'methoddbRequest' in config file define if method must be GET or Post
	  switch(AppInterface.getPropertyByName("methoddbRequest")) 
	  {
	    case "GET": 
	      request=new GetDbMethod();
	      break;
	    case "POST": 
	      request=new PostDbMethod();
	      break;
	    default: 
	      request=new GetDbMethod();
	      break;
	  }
	  
	  request.CreateRequest(idDevice, idMeasure, value);
	  
	  try {
		     //Print in window request
		     AppInterface.PrintRequest(request.getDescMethod()+"=" + request.GetRequestBase().getURI());
		     
		     HttpResponse response = httpclient.execute(request.GetRequestBase());
		    
		     //Having a response print this
		 	 // Get hold of the response entity
		     HttpEntity entity = response.getEntity();

		       // If the response does not enclose an entity, there is no need
		       // to bother about connection release
		       if (entity != null) 
		       {
		    	  AppInterface.PrintResponse(response);

	           } 
	      }
	  catch (ClientProtocolException e) 
	      {
		     e.printStackTrace();    
	      } 
	  catch (IOException e) 
	      {
		     e.printStackTrace();
	      }
   }
   
} 
